package controller;

import dao.GroupChatDao;

public class GroupChatController {

    private final GroupChatDao groupChatDao = new GroupChatDao();

    public boolean addGroup(String name) {
        if (groupChatDao.searchGroup(name)) {
            if (groupChatDao.checkGroup(name)) {
                groupChatDao.updateGroup(name);
                return true;
            } else {
                return false;
            }
        } else {
            groupChatDao.addGroup(name);
            return true;
        }
    }

    public String[] getAllGroupNames() {
        return groupChatDao.getGroupName();
    }

    public boolean userAddGroup(String username, String groupName) {
        if (groupChatDao.selectUserName(groupName)) {
            if (groupChatDao.checkUser(username, groupName)) {
                groupChatDao.updateUser(username, groupName);
                groupChatDao.updateUser(username, groupName);
                return true;
            } else if (groupChatDao.select(username, groupName)) {
                return false;
            } else {
                groupChatDao.userAddGroup(username, groupName);
                return true;
            }
        }
        return false;
    }

    public String[] getUserGroups(String username) {
        return groupChatDao.getUserGroupName(username);
    }

    public String[] getGroupUsers(String name) {
        return groupChatDao.getGroupUserName(name);
    }
}